<?php

/**
 * categories actions.
 *
 * @package    sf_sandbox
 * @subpackage categories
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class categoriesActions extends sfActions 
{ 
  public function executeIndex() 
  { 
    $this->categoriesList = CategoriesPeer::doSelect(new Criteria()); 
  } 

  public function executeCreate() 
  { 
    $this->form = new CategoriesForm();  
    $this->setTemplate('edit'); 
  }  

  public function executeEdit($request) 
  { 
    $this->form = new CategoriesForm(CategoriesPeer::retrieveByPk($request->getParameter('categories_id'))); 
  }  

  public function executeUpdate($request) 
  { 
    $this->forward404Unless($request->isMethod('post'));  
    $this->form = new CategoriesForm(CategoriesPeer::retrieveByPk($request->getParameter('categories_id')));  
    $this->form->bind($request->getParameter('categories'), $request->getFiles('categories'));  
    if ($this->form->isValid()) 
    { 
      $this->form->save();
      $categories = $this->form->getObject();
      $file = $this->form->getValue('categories_image');
      $filename = $categories->getCategoriesId().$file->getExtension($file->getOriginalExtension());
      $file->save(sfConfig::get('sf_upload_dir').'/categories/originals/'.$filename);
      $categories->setCategoriesImage($filename);
      $categories->save();
      $thumbnail = new sfThumbnail(150, 150);
      $thumbnail->loadFile(sfConfig::get('sf_upload_dir').'/categories/originals/'.$filename);
      $thumbnail->save(sfConfig::get('sf_upload_dir').'/categories/thumbnails/'.$filename);
      $this->redirect('categories/edit?categories_id='.$categories->getCategoriesId());
    }
    $this->setTemplate('edit'); 
  }  

  public function executeDelete($request) 
  { 
    $this->forward404Unless($categories = CategoriesPeer::retrieveByPk($request->getParameter('categories_id')));  
    $categories->delete();  $this->redirect('categories/index'); 
  } 
} 